package fy.uap.operatorImpl;

import java.util.ArrayList;
import java.util.List;

import com.seeyon.oainterface.exportData.flow.FlowExport;
import com.seeyon.oainterface.exportData.form.ValueExport;

import fy.form.listen.dao.MyOrgManager;
import fy.form.listen.log.LogWriter;
import fy.form.listen.operate.AForm_Save_Operator;
import fy.form.listen.set.OA_UAPAccountContrast;
import fy.uap.bean.InvalidProviderBean;
import fy.uap.bean.InvalidProviderFormBean;
import fy.uap.bean.RangeBean;
import fy.uap.dao.InvalidProviderFormDao;
import fy.util.MyUtil;

public class InvalidProviderFormOperator extends AForm_Save_Operator {

	private fy.uap.bean.InvalidProviderFormBean ipfb = new InvalidProviderFormBean();

	private List<String> providerCodeList = new ArrayList<String>();

	private List<String> invalidDateList = new ArrayList<String>();

	private List<String> companyList = new ArrayList<String>();

	// 合格
	private List<String> qType1List = new ArrayList<String>();
	private List<String> qType2List = new ArrayList<String>();
	private List<String> qType3List = new ArrayList<String>();

	private List<String> qTypeName1List = new ArrayList<String>();
	private List<String> qTypeName2List = new ArrayList<String>();
	private List<String> qTypeName3List = new ArrayList<String>();

	// 潜在
	private List<String> pType1List = new ArrayList<String>();
	private List<String> pType2List = new ArrayList<String>();
	private List<String> pType3List = new ArrayList<String>();

	private List<String> pTypeName1List = new ArrayList<String>();
	private List<String> pTypeName2List = new ArrayList<String>();
	private List<String> pTypeName3List = new ArrayList<String>();

	private long memberID;

	public String getFormName() {
		return "供应商导出批准表";
	}

	public boolean save() {
		ProviderMessageManager pmm = new ProviderMessageManager();
		pmm.setMemberID(memberID);
		if (pmm.checkMemberIDIsExists(ipfb.getMemberCode())) {

			ipfb.setpTypeList(getSupplierType(pType1List, pType2List,
					pType3List, 2));
			ipfb.setqTypeList(getSupplierType(qType1List, qType2List,
					qType3List, 1));
			ipfb.setCompanyList(companyList);
			List<InvalidProviderBean> list = getInvalidProviderList();
			if (list != null
					&& (ipfb.getpTypeList() != null
							&& ipfb.getpTypeList().size() != 0 || ipfb
							.getqTypeList() != null
							&& ipfb.getqTypeList().size() != 0)) {

				ipfb.setInvalidProviderList(list);

				InvalidProviderFormDao dao = new InvalidProviderFormDao();
				dao.setInvaliProviderFormBean(ipfb);

				if (dao.save()) {
					if (dao.getFailPRangeList() != null
							|| dao.getFailQRangeList() != null) {
						StringBuilder sb = new StringBuilder()
								.append("《")
								.append(super.formBean.getFlowExport()
										.getFlowTitle())
								.append("》存在未写入UAP的项，单据编号：")
								.append(ipfb.getNumber()).append("。");
						List<RangeBean> rangeList = dao.getFailPRangeList();
						if (rangeList != null && rangeList.size() > 0) {
							sb.append("\n 未导出的潜在供应商：\n");
							for (int i = 0, size = rangeList.size(); i < size; i++) {
								sb.append("\n")
										.append(OA_UAPAccountContrast
												.getAccountNameByUapID(rangeList
														.get(i).getPk_corp()));
								sb.append("   ")
										.append(ipfb.getpTypeName(rangeList
												.get(i).getPk_invclass()))
										.append("   供应商编码：")
										.append(rangeList.get(i).getPk_vendor())
										.append("；");
							}
						}

						rangeList = dao.getFailQRangeList();
						if (rangeList != null && rangeList.size() > 0) {
							sb.append("\n 未导出的潜在供应商：\n");
							for (int i = 0, size = rangeList.size(); i < size; i++) {
								sb.append("\n")
										.append(OA_UAPAccountContrast
												.getAccountNameByUapID(rangeList
														.get(i).getPk_corp()));
								sb.append("   ")
										.append(ipfb.getqTypeName(rangeList
												.get(i).getPk_invclass()))
										.append("   供应商编码：")
										.append(rangeList.get(i).getPk_vendor())
										.append("；");
							}
						}

						pmm.sendMessage(sb.toString());
						LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
								sb.toString());

					}

					if (!dao.isWriteToErpSuccess()) {
						String msg = new StringBuilder()
								.append("《")
								.append(super.formBean.getFlowExport()
										.getFlowTitle())
								.append("》写入ERP失败，单据编号：")
								.append(ipfb.getNumber())
								.append("。原因：写入ERP数据库发生异常。请联系集团采购部相关人员，手工导入。")
								.toString();
						pmm.sendMessage(msg);
						LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
								msg);
					}

					return true;
				} else {
					String msg = new StringBuilder()
							.append("《")
							.append(super.formBean.getFlowExport()
									.getFlowTitle())
							.append("》写入UAP和ERP失败，单据编号：")
							.append(ipfb.getNumber())
							.append("。原因：写入UAP数据库发生异常。请联系集团采购部相关人员，手工导入。")
							.toString();
					pmm.sendMessage(msg);
					LogWriter
							.writeLog(MyUtil.getRealPath(this.getClass()), msg);
				}
			} else {
				String msg = new StringBuilder().append("《")
						.append(super.formBean.getFlowExport().getFlowTitle())
						.append("》写入UAP和ERP失败，单据编号：").append(ipfb.getNumber())
						.append("。原因：未获取到失效的供货范围。请联系集团采购部相关人员，手工导入。")
						.toString();
				pmm.sendMessage(msg);
				LogWriter.writeLog(MyUtil.getRealPath(this.getClass()), msg);
			}
		} else {
			String msg = new StringBuilder().append("《")
					.append(super.formBean.getFlowExport().getFlowTitle())
					.append("》写入UAP和ERP失败，单据编号：").append(ipfb.getNumber())
					.append("。原因：填单人员工号为空。").toString();
			pmm.sendMessage(msg);
			LogWriter.writeLog(MyUtil.getRealPath(this.getClass()), msg);
		}
		return false;
	}

	private List<InvalidProviderBean> getInvalidProviderList() {
		List<InvalidProviderBean> list = null;
		if (providerCodeList.size() == invalidDateList.size()) {

			for (int i = 0, size = providerCodeList.size(); i < size; i++) {

				InvalidProviderBean bean = new InvalidProviderBean();
				bean.setProviderCode(providerCodeList.get(i));
				bean.setInvalidDate(invalidDateList.get(i));

				if (list == null) {
					list = new ArrayList<InvalidProviderBean>();
				}

				list.add(bean);
			}
		}
		return list;
	}

	private List<String> getSupplierType(List<String> type1List,
			List<String> type2List, List<String> type3List, int sign) {
		List<String> typeList = null;
		int size = 0;
		if ((size = type1List.size()) == type2List.size()) {
			if (size == type3List.size()) {
				if (size > 0) {
					for (int i = 0; i < size; i++) {
						String type = type3List.get(i);
						int index = 3;
						if (type == null || "".equals(type)) {
							type = type2List.get(i);
							index = 2;
							if (type == null || "".equals(type)) {
								type = type1List.get(i);
								index = 1;
							}

						}
						if (type != null && !"".equals(type)) {
							if (typeList == null) {
								typeList = new ArrayList<String>();
								typeList.add(type);
							} else if (typeList.indexOf(type) == -1) {
								typeList.add(type);
							}
							if (sign == 1) {
								if (index == 1) {
									ipfb.setqTypeMap(type,
											qTypeName1List.get(i));
								} else if (index == 2) {
									ipfb.setqTypeMap(type,
											qTypeName2List.get(i));
								} else if (index == 3) {
									ipfb.setqTypeMap(type,
											qTypeName3List.get(i));
								}
							} else if (sign == 2) {
								if (index == 1) {
									ipfb.setpTypeMap(type,
											pTypeName1List.get(i));
								} else if (index == 2) {
									ipfb.setpTypeMap(type,
											pTypeName2List.get(i));
								} else if (index == 3) {
									ipfb.setpTypeMap(type,
											pTypeName3List.get(i));
								}
							}
						}
					}
				}

			}
		}
		return typeList;
	}

	@Override
	protected void parseOtherField_FirstStep(
			com.seeyon.v3x.services.flow.bean.FlowExport flowExport) {
		ipfb.setApplicationDate(flowExport.getFlowCreateTime());
		memberID = flowExport.getFlowCreatePerson().getId();
		ipfb.setMemberCode(MyOrgManager.getInstance().getMemberByID(memberID)
				.getCode());
	}

	@Override
	protected void parseValueExport(
			com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String name = ve.getDisplayName();
		String value = ve.getValue();

		if ("导出制造部选择器".equals(name)) {
			String uapAccount = OA_UAPAccountContrast.getUapAccountID(value);
			if (!setCompany(uapAccount))
				if (uapAccount != null && companyList.indexOf(uapAccount) == -1) {
					companyList.add(uapAccount);
				}
		} else if ("失效原因".equals(name)) {
			ipfb.setReason(value);
		} else if ("合格供应商_失效范围1PK".equals(name)) {
			qType1List.add(value);
		} else if ("合格供应商_失效范围2PK".equals(name)) {
			qType2List.add(value);
		} else if ("合格供应商_失效范围3PK".equals(name)) {
			qType3List.add(value);
		} else if ("潜在供应商_失效范围1PK".equals(name)) {
			pType1List.add(value);
		} else if ("潜在供应商_失效范围2PK".equals(name)) {
			pType2List.add(value);
		} else if ("潜在供应商_失效范围3PK".equals(name)) {
			pType3List.add(value);
		} else if ("合格供应商_失效范围1".equals(name)) {
			qTypeName1List.add(value);
		} else if ("合格供应商_失效范围2".equals(name)) {
			qTypeName2List.add(value);
		} else if ("合格供应商_失效范围3".equals(name)) {
			qTypeName3List.add(value);
		} else if ("潜在供应商_失效范围1".equals(name)) {
			pTypeName1List.add(value);
		} else if ("潜在供应商_失效范围2".equals(name)) {
			pTypeName2List.add(value);
		} else if ("潜在供应商_失效范围3".equals(name)) {
			pTypeName3List.add(value);
		} else if ("处理意见".equals(name)) {
			ipfb.setOpinion(value);
		} else if ("供应商编号".equals(name)) {
			providerCodeList.add(MyUtil.clearEmpty(value));
		} else if ("失效日期".equals(name.indexOf(" ") > -1 ? name.split(" ")[0]
				: name)) {
			invalidDateList.add(MyUtil.getDate(value));
		} else if ("单据编号".equals(name)) {
			ipfb.setNumber(value);
		} else if (OA_UAPAccountContrast.getUapShortNameList().indexOf(name) != -1) {
			if ("1".equals(value)) {
				String uapAccount = OA_UAPAccountContrast
						.getUapAccountIDByUAPShortName(name);
				if (!setCompany(uapAccount)) {
					if (uapAccount != null
							&& companyList.indexOf(uapAccount) == -1) {
						companyList.add(uapAccount);
					}
				}
			}
		}
	}

	private boolean setCompany(String uapAccount) {
		// 福清万达/巴士/包边/福耀四公司供应商导入共享
		if ("1004".equals(uapAccount) || "1005".equals(uapAccount)) {
			if (companyList.indexOf("1004") == -1) {
				companyList.add("1004");
			}
			if (companyList.indexOf("1005") == -1) {
				companyList.add("1005");
			}
			if (companyList.indexOf("1024") == -1) {
				companyList.add("1024");
			}
			if (companyList.indexOf("1025") == -1) {
				companyList.add("1025");
			}
			return true;
		} else if ("1013".equals(uapAccount) || "1014".equals(uapAccount)) {
			// 长春轿车/ 巴士公司供应商导入共享
			if (companyList.indexOf("1013") == -1) {
				companyList.add("1013");
			}
			if (companyList.indexOf("1014") == -1) {
				companyList.add("1014");
			}
			return true;
		} else if ("1006".equals(uapAccount) || "1026".equals(uapAccount)) {
			// 广州汽车玻璃/广州佛山汽车玻璃导入共享
			if (companyList.indexOf("1006") == -1) {
				companyList.add("1006");
			}
			if (companyList.indexOf("1026") == -1) {
				companyList.add("1026");
			}
			return true;
		} else if ("1009".equals(uapAccount) || "1032".equals(uapAccount)) {
			// 湖北/武汉荆门导入共享
			if (companyList.indexOf("1032") == -1) {
				companyList.add("1032");
			}
			if (companyList.indexOf("1009") == -1) {
				companyList.add("1009");
			}
			return true;
		}else if ("1012".equals(uapAccount)) {
			// 北京 导入保定
			if (companyList.indexOf("1036") == -1) {
				companyList.add("1036");
			}
			return true;
		}
		return false;
	}
}
